package com.shilei.chainOfResponsibility.v1;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 责任链   request  和 response 处理顺序一样
 *                      如何 将response的执行顺序倒过来呢？？？
 * @author: 81271
 * @date: 2020-04-21 20:35
 */
public class Servlet_Main {
    public static void main(String[] args) {
        Request request = new Request();
        request.str = "<p> 1231231245爱上东南季风还的撒啊</p>";
        Response response = new Response();
        response.str = "";

        FilterChain f = new FilterChain();
        f.add(new NumberFilter()).add(new HtmlFilter());
        f.doFilter(request, response);
        System.out.println(request.str);
        System.out.println(response.str);

    }
}

interface Filter {

    boolean doFilter(Request request, Response response);
}

class NumberFilter implements Filter {

    @Override
    public boolean doFilter(Request request, Response response) {
        request.str = request.str.replaceAll("123","一二三");
        response.str += "----NumberFilter()";
        return true;
    }
}

class HtmlFilter implements Filter {

    @Override
    public boolean doFilter(Request request, Response response) {
        request.str = request.str.replaceAll("<","[").replaceAll(">","]");
        response.str += "----HtmlFilter()";
        return true;
    }
}


class Request {
    String str;
}


class Response {
    String str;
}

class FilterChain implements Filter {
    private List<Filter> filterList = new ArrayList<>();

    public FilterChain add (Filter filter) {
        filterList.add(filter);
        return this;
    }

    @Override
    public boolean doFilter(Request request, Response response) {

        for (Filter filter: filterList) {
            filter.doFilter(request, response);
        }
        return true;
    }
}

